//
// GPSMapEdit
// (c) Konstantin Galichsky (kg@geopainting.com), 2006-2007
//
// Object properties dialog - 'Info' page.
//

# include "StdAfx.h"
# include "Resource.h"
# include "RgnProperties.h"
# include "Map.h"
# include "Types.h"
# include "SetRgnExtra.h"
# include "NearestRgnsList.h"
# include "Settings.h"
# include "Button.h"

bool CRgnInfoPage::OnInit (HWND _hwndFocus) {
	if (m_rgn.pExtra) {
		SetDlgItemText (IDC_PHONE,       m_rgn.pExtra->strPhone      .c_str ());
		SetDlgItemText (IDC_FAX,         m_rgn.pExtra->strFax        .c_str ());
		SetDlgItemText (IDC_EMAIL,       m_rgn.pExtra->strEmail      .c_str ());
		SetDlgItemText (IDC_WEBPAGE,     m_rgn.pExtra->strWebPage    .c_str ());
		SetDlgItemText (IDC_DESCRIPTION, m_rgn.pExtra->strDescription.c_str ());
	}

	SetButtonIcon (m_hWnd, IDC_NEAREST_PHONE, IDI_DOWN);
	SetButtonIcon (m_hWnd, IDC_NEAREST_FAX,   IDI_DOWN);

	if (! g_map.pTypeSet->pfHasPhone (m_rgn)) {
		EnableDlgItem (IDC_PHONE,         false);
		EnableDlgItem (IDC_NEAREST_PHONE, false);
	}
	if (! g_map.pTypeSet->pfHasContact (m_rgn)) {
		EnableDlgItem (IDC_FAX,           false);
		EnableDlgItem (IDC_NEAREST_FAX,   false);
		EnableDlgItem (IDC_EMAIL,         false);
		EnableDlgItem (IDC_WEBPAGE,       false);
	}
	if (! g_map.pTypeSet->pfHasDescription (m_rgn))
		EnableDlgItem (IDC_DESCRIPTION,   false);

	return true;
}

void CRgnInfoPage::OnCommand (WORD _wCmd, WORD _wNotifyCode, HWND _hwndCtl) {
	switch (_wCmd) {
		case IDC_PHONE:
		case IDC_FAX:
		case IDC_EMAIL:
		case IDC_WEBPAGE:
		case IDC_DESCRIPTION:
			if (_wNotifyCode == EN_UPDATE)
				PropSheetChanged ();
			break;

		case IDC_GO_WEB: {
			string_t strURL;
			GetDlgItemText (IDC_WEBPAGE, strURL);
			if (! strURL.empty ())
				::ShellExecute (m_hWnd, NULL, strURL.c_str (), NULL, NULL, SW_SHOWNORMAL);
			break;
		}

		case IDC_NEAREST_PHONE:
			ShowNearestRgnsList (m_hWnd, _hwndCtl, IDC_PHONE, nrPhone, point_t (m_rgn.rectBound.MeanX (), m_rgn.rectBound.MeanY ()), & m_rgn);
			break;
		case IDC_NEAREST_FAX:
			ShowNearestRgnsList (m_hWnd, _hwndCtl, IDC_FAX,   nrPhone, point_t (m_rgn.rectBound.MeanX (), m_rgn.rectBound.MeanY ()), & m_rgn);
			break;
	}
}

void CRgnInfoPage::OnApply (const PSHNOTIFY *) {
	if (g_map.pTypeSet->pfHasPhone (m_rgn)) {
		// Phone.
		const HWND hPhone = GetDlgItem (IDC_PHONE);
		const size_t cPhoneLen = ::GetWindowTextLength (hPhone);
		char * const strPhone = reinterpret_cast<char *> (alloca (cPhoneLen + 1));
		::GetWindowText (hPhone, strPhone, cPhoneLen + 1);
		if (! (m_rgn.pExtra->strPhone == strPhone))
			SetObjectPhone (m_rgn, strPhone);
	}

	if (g_map.pTypeSet->pfHasContact (m_rgn)) {
		// Fax.
		const HWND hFax = GetDlgItem (IDC_FAX);
		const size_t cFaxLen = ::GetWindowTextLength (hFax);
		char * const strFax = reinterpret_cast<char *> (alloca (cFaxLen + 1));
		::GetWindowText (hFax, strFax, cFaxLen + 1);
		if (! (m_rgn.pExtra->strFax == strFax))
			SetObjectFax (m_rgn, strFax);

		// Email.
		const HWND hEmail = GetDlgItem (IDC_EMAIL);
		const size_t cEmailLen = ::GetWindowTextLength (hEmail);
		char * const strEmail = reinterpret_cast<char *> (alloca (cEmailLen + 1));
		::GetWindowText (hEmail, strEmail, cEmailLen + 1);
		if (! (m_rgn.pExtra->strEmail == strEmail))
			SetObjectEmail (m_rgn, strEmail);

		// Web.
		const HWND hWebPage = GetDlgItem (IDC_WEBPAGE);
		const size_t cWebPageLen = ::GetWindowTextLength (hWebPage);
		char * const strWebPage = reinterpret_cast<char *> (alloca (cWebPageLen + 1));
		::GetWindowText (hWebPage, strWebPage, cWebPageLen + 1);
		if (! (m_rgn.pExtra->strWebPage == strWebPage))
			SetObjectWebPage (m_rgn, strWebPage);
	}

	if (g_map.pTypeSet->pfHasDescription (m_rgn)) {
		// Description.
		const HWND hDesc = GetDlgItem (IDC_DESCRIPTION);
		const size_t cDescLen = ::GetWindowTextLength (hDesc);
		char * const strDesc = reinterpret_cast<char *> (alloca (cDescLen + 1));
		::GetWindowText (hDesc, strDesc, cDescLen + 1);
		if (! (m_rgn.pExtra->strDescription == strDesc))
			SetObjectDescription (m_rgn, strDesc);
	}
}
